﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Catering.yl.Daily
{
    public partial class DMMAccount : System.Web.UI.Page
    { 
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {                
                txt_PQYRQ.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy/MM/dd'})");
                txt_PCCRQ.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy/MM/dd'})");
                txt_PQYRQ.Text = DateTime.Today.ToString("yyyy/MM/dd");
                txt_PCCRQ.Text = DateTime.Today.ToString("yyyy/MM/dd");
                LoadForm();                
            }            
        }
        public void LoadForm()
        {
            string useDate = null;
            string currentClassID = null;
            string currentGroupID = null;
            if (Session["useDate"] != null)
                useDate = Session["useDate"].ToString();
            if (Session["ClassID"] != null)
                currentClassID = Session["ClassID"].ToString();
            if (Session["GroupID"] != null)
                currentGroupID = Session["GroupID"].ToString();
            try
            {
                DataTable dt_Plan = new Catering_Rules.DMMAccount().GetList("ClassID = " + currentClassID).Tables[0];
                dt_Plan.Columns.Add("tempId");
                dt_Plan.Columns.Add("p_ccrq");
                dt_Plan.Columns.Add("p_qyrq");

                for (int i = 0; i < dt_Plan.Rows.Count; i++)
                {
                    dt_Plan.Rows[i]["tempId"] = i + 1;
                    if (dt_Plan.Rows[i][5].ToString() != "")
                        dt_Plan.Rows[i]["p_ccrq"] = Convert.ToDateTime(dt_Plan.Rows[i][5].ToString()).ToString("yyyy/MM/dd");
                    if (dt_Plan.Rows[i][6].ToString() != "")
                        dt_Plan.Rows[i]["p_qyrq"] = Convert.ToDateTime(dt_Plan.Rows[i][6].ToString()).ToString("yyyy/MM/dd");
                    for (int Cols = 0; Cols < dt_Plan.Columns.Count; Cols++)
                    {
                        if (dt_Plan.Rows[i][Cols].ToString() == "")
                            dt_Plan.Rows[i][Cols] = "&nbsp";
                    }
                }
                this.Rep_Plan.DataSource = dt_Plan;
                this.Rep_Plan.DataBind();
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }
        [WebMethod]
        public static string Add(string PName, string PType, string PManu, string PCCBH, string PCCRQ, string PQYRQ, string PZCBH, string PZJNX, string PYJZ, string PJSZK, string PAZDD, string PUser, string hdn_useDate, string hdn_currentClassID, string hdn_currentGroupID)
        {
            Catering_Model.DMMAccount _cp = new Catering_Model.DMMAccount();            
            _cp.PName = PName;
            _cp.PType = PType;
            _cp.PManu = PManu;
            _cp.PCCBH = PCCBH;
            _cp.PCCRQ = Convert.ToDateTime(PCCRQ).Date;
            _cp.PQYRQ = Convert.ToDateTime(PQYRQ).Date;
            _cp.PZCBH = PZCBH;
            _cp.PZJNX = PZJNX;
            _cp.PYJZ = PYJZ;
            _cp.PJSZK = PJSZK;
            _cp.PAZDD = PAZDD;
            _cp.PUser = PUser;
            _cp.ClassID = hdn_currentClassID;
            _cp.GroupID = hdn_currentGroupID;

            new Catering_Rules.DMMAccount().Add(_cp);
            return "OK";
        }
        [WebMethod]
        public static ArrayList Get(string _id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.DMMAccount md = new Catering_Rules.DMMAccount().GetModel(Convert.ToInt32(_id));
            al.Add(md.Id);
            al.Add(md.PName);
            al.Add(md.PType);
            al.Add(md.PManu);
            al.Add(md.PCCBH);
            al.Add(md.PCCRQ.ToShortDateString());
            al.Add(md.PQYRQ.ToShortDateString());
            al.Add(md.PZCBH);
            al.Add(md.PZJNX);
            al.Add(md.PYJZ);
            al.Add(md.PJSZK);
            al.Add(md.PAZDD);
            al.Add(md.PUser);
            return al;
        }
        [WebMethod]
        public static string Edit(string _id, string PName, string PType, string PManu, string PCCBH, string PCCRQ, string PQYRQ, string PZCBH, string PZJNX, string PYJZ, string PJSZK, string PAZDD, string PUser)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(_id, out id);
            if (id <= 0) return "NO";

            Catering_Model.DMMAccount _CE = new Catering_Model.DMMAccount();

            _CE.Id = id;
            _CE.PName = PName;
            _CE.PType = PType;
            _CE.PManu = PManu;
            _CE.PCCBH = PCCBH;
            _CE.PCCRQ = DateTime.Parse(PCCRQ); ;
            _CE.PQYRQ = DateTime.Parse(PQYRQ);
            _CE.PZCBH = PZCBH;
            _CE.PZJNX = PZJNX;
            _CE.PYJZ = PYJZ;
            _CE.PJSZK = PJSZK;
            _CE.PAZDD = PAZDD;
            _CE.PUser = PUser;
            new Catering_Rules.DMMAccount().Update(_CE);

            return "OK";
        }
        protected void Rep_Plan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.DMMAccount().Delete(delID))
                {
                    this.LoadForm();
                }
            }
        }
    }
}